import request from '@/utils/request'

interface FetchCommunitiesResponse {
  totalCommunities: number
  items: string[]
}

/**
 * 获取社区列表
 * @param params 请求参数
 * @returns
 */
export const fetchCommunities = (): Promise<FetchCommunitiesResponse> => {
  return request({
    url: '/history/communities',
    method: 'get',
  })
}

interface FetchSingleTestRecordsParams {
  page: number
  size: number
  community_code: string
  name_contains: string
  mrn_contains: string
}
export interface Patient {
  name: string
  medical_record_number: string
}

interface FetchPatientsResponse {
  total_items: number
  items: Patient[]
  page: number
  size: number
  total_pages: number
}

/**
 * 获取患者列表
 * @param params 请求参数
 * @returns
 */
export const fetchPatients = (
  params: FetchSingleTestRecordsParams,
): Promise<FetchPatientsResponse> => {
  return request({
    url: '/history/patients/query',
    method: 'post',
    data: params,
  })
}

export interface LogDataRecord {
  log_id: number
  medical_record_number: string
  created_at: string
  created_date: string
  status: string
  gait_analysis: any
  facial_expression: any
  eye_tracking: any
  handwriting: any
  speech_analysis: any
  semantic_analysis: any
  raw_result_path: string | null
}

interface LogDataResponse {
  items: LogDataRecord[]
  total_items: number
  page: number
  size: number
  total_pages: number
}

interface LogDataRequest {
  page: number
  size: number
  medical_record_number: string
  start_date?: string | undefined
  end_date?: string | undefined
  status: string
  sort_by?: string | undefined
  sort_order?: string | undefined
}

export const fetchSingleTestRecords = (params: LogDataRequest): Promise<LogDataResponse> => {
  return request({
    method: 'post',
    url: '/history/records/by_patient',
    data: params,
  })
}

export const fetchRecordDetails = (
  medical_record_number: string,
  created_date: string,
): Promise<LogDataRecord> => {
  return request({
    method: 'get',
    url: `/history/records/${medical_record_number}/${created_date}`,
  })
}
